package daoIpmt;

import java.sql.SQLException;
import java.util.List;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import beans.Member;
import beans.PersonInformation;
import beans.Searcher;
import dao.SearchDao;
import utils.DataSourceUtils;

public class SearchIpmt implements SearchDao {

	@Override
	public List<PersonInformation> findMemberBySearch(Searcher searcher) {
		DataSourceUtils utils = new DataSourceUtils();
		String sql="select*from personinformation where sex=? and birthday>? and birthday<? and income=? and marital=? and city=? and high=?";
		QueryRunner que = new QueryRunner(utils.getDataSource());
		try {
			List<PersonInformation> list = que.query(sql, new BeanListHandler<PersonInformation>(PersonInformation.class),searcher.getSex(),
					searcher.getDateStart(),searcher.getDateEnd(),searcher.getIncome(),searcher.getMarital(),searcher.getCity(),searcher.getHigh());
		return list;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

	@Override
	public List<PersonInformation> findMemberByGendar(String gendar) {
		DataSourceUtils utils = new DataSourceUtils();
		String sql="select *  from personinformation where  sex=? order by heat desc limit 0,6";
		QueryRunner que = new QueryRunner(utils.getDataSource());
		try {
			List<PersonInformation> list = que.query(sql, new BeanListHandler<PersonInformation>(PersonInformation.class),gendar);
		return list;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;		
	}

	public PersonInformation findMemberByUsername(String followusername) {
		DataSourceUtils utils = new DataSourceUtils();
		String sql="select *  from personinformation where  username = ?";
		QueryRunner que = new QueryRunner(utils.getDataSource());
		try {
			PersonInformation personInformation = que.query(sql, new BeanHandler<PersonInformation>(PersonInformation.class),followusername);
		return personInformation;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;		
	}

}
